package com.longrise.android.byjk.plugins.aboutme.personalInfo.product_show;

import com.longrise.LEAP.Base.Objects.EntityBean;
import com.longrise.android.byjk.common.UrlConstants;
import com.longrise.android.byjk.model.UserInfor;
import com.longrise.android.loaddata.newloaddata.LoadDataManagerFather;
import com.longrise.common.datasource.remote.LoadDataManager;
import com.longrise.common.utils.PrintLog;

import java.util.ArrayList;
import java.util.List;

import static com.bumptech.glide.gifdecoder.GifHeaderParser.TAG;

/**
 * Created by Administrator on 2018/3/9.
 * 产品方案presenter
 */

public class ProductProgramPresenter extends ProductProgramContract.Presenter {


    @Override
    protected void init() {

    }

    //查询产品方案
    @Override
    public void getProductProData() {

        String usersfzh = UserInfor.getInstance().getUsersfzh();
        String userid = UserInfor.getInstance().getUserid();
        EntityBean bean = new EntityBean();
        bean.set("cardno", usersfzh);
        bean.set("userid ", userid);
        EntityBean bean1 = new EntityBean();
        bean1.set("bean", bean);
        mView.showNormalLoadingPage();
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "bbt_user_sInsuranceProduct", bean1, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int resultstate = bean.getInt("resultstate");
                    if (resultstate == 1) {
                        parseProductData(bean);
                    } else {
                        mView.showToast(bean.getString("resultdesc"));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {
                mView.showContentPage();
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    //删除产品方案
    @Override
    public void deleteProductData(List<String> productIds) {

        EntityBean honorBean = new EntityBean();
        EntityBean bean = new EntityBean();
        bean.set("id", productIds);
        bean.set("userid", UserInfor.getInstance().getUserid());
        bean.set("cardno", UserInfor.getInstance().getUsersfzh());
        bean.set("delecttype", "2");
        honorBean.set("bean", bean);
        mView.showLoadingDialog();
        LoadDataManager.getInstance().callService2(null, UrlConstants.BaseUrl, "insh_user_deletemethod", honorBean, new LoadDataManager.OnRequestCallListener2() {

            @Override
            public void onSuccess(String key, String service, Object result) {
                try {
                    EntityBean bean = (EntityBean) result;
                    int resultstate = bean.getInt("resultstate");
                    if (resultstate == 1) {
                        mView.showToast(bean.getString("resultdesc"));
                        mView.responseDelProduct(bean);
                    } else {
                        mView.showToast(bean.getString("resultdesc"));
                    }

                } catch (Exception e) {
                    e.printStackTrace();
                }
            }


            @Override
            public void onFinished(String key, String service) {
                mView.dismissLoadingDialog();
            }

            @Override
            public void onError(String key, String service, LoadDataManagerFather.ResultType resultType) {

            }

        });
    }

    public void parseProductData(EntityBean bean) {
        EntityBean[] results = bean.getBeans("result");
        List<ProductProBean> mProductList = new ArrayList<>();
        if (results != null && results.length != 0) {
            for (EntityBean resultBean : results) {
                ProductProBean productBean = new ProductProBean();
                productBean.setProductId(resultBean.getString("productid"));
                productBean.setProductName(resultBean.getString("productname"));
                productBean.setProductdesc(resultBean.getString("productdesc"));
                mProductList.add(productBean);
            }
        }
        PrintLog.e(TAG, "mProductList=:" + mProductList);
        mView.refreshData(mProductList);
    }
}
